package gupao.concurrency.lock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

@Slf4j
public class SynchronizedNonFair {

    static Object obj = new Object();

    public static void main(String[] args) throws InterruptedException {
        testNonFair();
    }

    static void testNonFair() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        synchronized (obj) {
            log.info("{} enter monitor..", Thread.currentThread().getName());
            new Thread(() -> {
                log.info("{} acquire monitor..", Thread.currentThread().getName());
                synchronized (obj) {
                    log.info("{} enter monitor..", Thread.currentThread().getName());
                }
                log.info("{} exit monitor..", Thread.currentThread().getName());
                latch.countDown();
            }, "acquired-thread-1").start();
        }
        log.info("{} exit monitor..", Thread.currentThread().getName());

        synchronized (obj){
            log.info("{} enter monitor..", Thread.currentThread().getName());
        }
        log.info("{} exit monitor..", Thread.currentThread().getName());

        latch.await();
        log.info("completed.");
    }
}
